<?php
class Book extends IController implements adminAuthorization
{
    public $checkRight  = 'all';
    public $layout = 'admin';
    public $data = array();

	public function init()
	{

	}

    public function author() 
    {
        // 处理搜索参数
        $search = IFilter::act(IReq::get('search'));
        // 处理分页参数（默认第一页）
        $page = IReq::get('page') ? IFilter::act(IReq::get('page'), 'int') : 1;
        // 初始化查询对象
        $query = new IQuery('author');
        // 设置搜索条件（示例：根据作者名称模糊搜索）
        if($search){
            $query->where = "author_name LIKE '%{$search}%'";
        }
        // 设置排序（按创建时间降序）
        $query->order = 'create_time desc';
        // 设置分页
        $query->page = $page;
        // 执行查询获取数据
        $authorList = $query->find();
        // 传递数据到视图
        $this->authorList = $authorList;
        $this->redirect('author');
    }

    /**
     * 添加作者
     * @author kangkai
     * url: http://wenku.me/index.php?controller=book&action=author_add
     */
    public function author_add()
    {
        if ($_POST) {
            $author_name = IFilter::act(IReq::input('author_name'));
            $author_intro = IFilter::act(IReq::input('author_intro'));
            $gender = IFilter::act(IReq::input('gender', 0)); // 默认0（未知）
            $nationality = IFilter::act(IReq::input('nationality', '')); // 默认空字符串
            $status = IFilter::act(IReq::input('status', 1)); // 默认1（启用）
            $author_info = array(
                'author_name' => $author_name,
                'author_intro' => $author_intro,
                'gender' => $gender,
                'nationality' => $nationality,
                'status' => $status
            );

            $tb_author = new IModel('author');
            $tb_author->setData($author_info);
            $author_id = $tb_author->add();
            if ($author_id) {
                $this->redirect('author');
            }
        } else {
            $this->redirect('author_add');
        }
        
    }

    public function author_edit() 
    {
        $author_id = IFilter::act($this->input('id')); // 获取要编辑的作者ID
        if(!$author_id){
            $this->error('作者ID不能为空');
            return;
        }
        
        $author_name = IFilter::act($this->input('author_name'));
        $author_intro = IFilter::act($this->input('author_intro'));
        $gender = IFilter::act($this->input('gender', 0)); // 默认0（未知）
        $nationality = IFilter::act($this->input('nationality', '')); // 默认空字符串
        $status = IFilter::act($this->input('status', 1)); // 默认1（启用）
        
        $author_info = array(
            'author_name' => $author_name,
            'author_intro' => $author_intro,
            'gender' => $gender,
            'nationality' => $nationality,
            'status' => $status
        );
        
        $tb_author = new IModel('author');
        $update_result = $tb_author->update($author_info, "id={$author_id}");
        
        if($update_result){
            $this->redirect('author'); // 更新成功重定向到列表
        }else{
            $this->error('更新作者信息失败');
        }
    }

    public function author_del() {}
}
